﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;

namespace FlickrPhoneSync
{

    public partial class FlickrPhoneSyncUI : Form
    {
        static void Main(string[] args)
        {
            FlickrPhoneSyncUI ui = new FlickrPhoneSyncUI();
            Application.ApplicationExit += new EventHandler(ui.OnApplicationExit);
            Application.Run(ui);
        }

        public static string STATE_FINISHED = "Finished";
        public static string STATE_TERMINATED = "Terminated";
        public static string STATE_TERMINATING = "Terminating...";
        public static string STATE_STARTED = "Started";
		
        private FlickrCon fc;
        private Dictionary<string, string> data;
        private static string SETTINGS_FILENAME = "settings.xml";
		
		/*###########################################################################*/
		/* ENTER YOUR API KEY HERE													 */
		/*###########################################################################*/
		private string API_KEY = "";
		private string API_SECRET = "";
		/*###########################################################################*/
		/*                                                                           */
		/*###########################################################################*/
		
        public FlickrPhoneSyncUI()
        {
            InitializeComponent();
        }

        private void FlickrPhoneSyncUI_Load(object sender, EventArgs e)
        {
            fc = new FlickrCon(API_KEY,API_SECRET);
            if (File.Exists(SETTINGS_FILENAME)){
                FileStream stream = new FileStream(SETTINGS_FILENAME,FileMode.Open);
                XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
                DataContractSerializer ser = new DataContractSerializer(typeof(Dictionary<string, string>));
                data = (Dictionary<string, string>)ser.ReadObject(reader);
                reader.Close();
                stream.Close();
                email.Text = data["email"];
                maxImages.Value = Convert.ToInt32(data["amount"]);
                target.Text = data["target"];
            }
            else
                data = new Dictionary<string, string>();
        }

        public void OnApplicationExit(Object sender, EventArgs e)
        {
            DataContractSerializer ser = new DataContractSerializer(typeof(Dictionary<string, string>));
            FileStream stream = new FileStream(SETTINGS_FILENAME, FileMode.Create);
            ser.WriteObject(stream, data);
            stream.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = true;
            try
            {
                fc.PrepareWorker(email.Text, new SetStatus(SetStatusToStrip), target.Text, Convert.ToInt32(maxImages.Value));
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "An error occoured:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            backgroundWorker1.RunWorkerAsync();
            toolStripStatusLabel1.Text = STATE_STARTED;
            data["email"] = email.Text;
            data["amount"] = Convert.ToString(maxImages.Value);
            data["target"] = this.target.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ThreadWorker worker = new ThreadWorker();
            Thread t = new Thread(new ThreadStart(worker.DoWork));
            t.SetApartmentState(ApartmentState.STA);    
            t.Start();
            t.Join();
            target.Text = worker.Directory;
        }

        public delegate void SetStatus(string s);

        public void SetStatusToStrip(string s)
        {
            Invoke(new SetStatus(SetStatusToStrip2),new Object[]{s});
        }

        public void SetStatusToStrip2(string s)
        {
            toolStripStatusLabel1.Text = s;

            if (s == STATE_FINISHED || s == STATE_TERMINATED )
            {
                button1.Enabled = true;
                button2.Enabled = true;
                button3.Enabled = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            fc.Kill();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            fc.DoWork(sender,e);
        }
    }
}
